package com.thrd.taiexhibition.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.thrd.taiexhibition.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/17
 */
public class LinearEnterpriseView extends LinearLayout implements View.OnClickListener {
    private int select=0;
    private Context context;
    private List<TextView> textViewList;
    public LinearEnterpriseView(Context context) {
        this(context,null);
    }

    public LinearEnterpriseView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LinearEnterpriseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        setOrientation(VERTICAL);
    }

    public void setStringList(List<String> stringList){
        textViewList=new ArrayList<>();
        for (int i=0;i<stringList.size() ; i++ ){
            LinearLayout.LayoutParams  mParams=   new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,1.0f);
            mParams.setMargins(0,5,0,5);
            TextView textView=new TextView(getContext());
            textView.setId(i);
            //文字居中
            textView.setGravity(Gravity.CENTER);
            //文字大小
            textView.setTextSize(getResources().getDimensionPixelSize(R.dimen.sp_6));
            if(i==select){
                textView.setTextColor(Color.parseColor("#08C4EE"));
                textView.setBackgroundColor(Color.parseColor("#5A5E6C"));
            }else{
                textView.setTextColor(Color.parseColor("#ffffff"));
                textView.setBackgroundColor(Color.parseColor("#00ffffff"));
            }
            //文字shez
            textView.setText(TextUtils.isEmpty(stringList.get(i))?"":stringList.get(i));
            textView.setOnClickListener(this);
            addView(textView,mParams);
            textViewList.add(textView);
        }

    }


    @SuppressLint("ResourceType")
    @Override
    public void onClick(View v) {
        if(v.getId()<4){
            textViewList.get(select).setBackgroundColor(Color.parseColor("#00ffffff"));
            textViewList.get(select).setTextColor(Color.parseColor("#ffffff"));
            textViewList.get(v.getId()).setTextColor(Color.parseColor("#08C4EE"));
            textViewList.get(v.getId()).setBackgroundColor(Color.parseColor("#5A5E6C"));
            select=v.getId();
        }
        if(onItemClickListene!=null){
            onItemClickListene.onItemClickListene(v.getId());
        }
    }

    public void setOnItemClickListene(OnItemClickListene onItemClickListene) {
        this.onItemClickListene = onItemClickListene;
    }

    private OnItemClickListene onItemClickListene;
    public interface OnItemClickListene{
        void onItemClickListene(int item);
    }
}
